<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="试题类型" prop="examType">
						<el-select v-model="form.examType">
							<el-option label="单选" value="0"></el-option>
							<el-option label="多选" value="1"></el-option>
							<el-option label="简述" value="2"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="试题题干" prop="title">
				<el-input v-model="form.title" placeholder="试题题干" clearable maxlength="80"></el-input>
			</el-form-item>
			<div>
				<el-form-item label="试题子项" prop="list" v-if="form.examType!='2'">
					<sc-form-table v-model="questionsList" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
						<el-table-column prop="optValue" label="选项" width="100">
							<template #default="scope">
								<el-input v-model="scope.row.optValue" placeholder="选项"></el-input>
							</template>
						</el-table-column>
						<el-table-column prop="childTitle" label="试题题支">
							<template #default="scope">
								<el-input v-model="scope.row.childTitle" placeholder="试题题支"></el-input>
							</template>
						</el-table-column>
					</sc-form-table>
				</el-form-item>
			</div>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="答案" prop="answer">
						<el-input v-model="form.answer" placeholder="答案" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="分值" prop="grade">
						<el-input-number v-model="form.grade" placeholder="答案" clearable></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注">
				<el-input v-model="form.remark" placeholder="备注" clearable type="textarea" maxlength="350"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
export default {
	emits: ['success', 'closed'],
	components: {},
	data() {
		return {
			questionsList: [],
			addTemplate: {
				optValue: '',
				childTitle: '',
			},
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '创建试题',
				edit: '试题编辑',
				show: '查看'
			},
			form: {
				recordId: '',
				title: '',
				sortId: '',
				questionsList: '',
				sortNo: '0',
				examType: "0",
				answer: '',
				grade: '',
				remark: '',
			},
			rules: {
				title: [
					{required: true, message: '试题题干不能为空', trigger: 'blur'}
				],
				answer: [
					{required: true, message: '答案不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.questionsList = JSON.stringify(this.questionsList);
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.exam.insertExamQuestions.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.exam.updateExamQuestions.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			if (this.mode == "add") {
				this.form.sortId = data.sortId
			} else {
				this.form.recordId = data.recordId
				this.form.title = data.title
				this.form.sortId = data.sortId
				this.form.questionsList = data.questionsList
				this.form.sortNo = data.sortNo
				this.form.examType = data.examType
				this.form.answer = data.answer
				this.form.grade = data.grade
				this.form.remark = data.remark
				if(data.questionsList!=null)
				{
					this.questionsList = JSON.parse(data.questionsList);
				}
			}
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
